/**
* Copyright (c) 2015, 爱wifi（版权声明）
*
* @file option.cpp
* @brief 文件的简单介绍
*
* 命令行参数
*
* @author: yuhaiyang
* @date: 2016/2/26
*/


#include <map>
#include <string>


#include "option.h"

using namespace std;


const char* const COptions::shortOptions = "h:p:t:w";


struct option COptions::longOptions[] = {
{ "host", required_argument, NULL, 'h' },
{ "port", required_argument, NULL, 'p' },
{ "type", required_argument, NULL, 't' },
{ "write", required_argument,NULL, 'w' }
};



//OptionMap MakeOption( int argc, char** argv )
//{
//    OptionMap options;
//    int c;
//    while((c = getopt_long (argc, argv, short_options, long_options, NULL)) != -1)
//    {
//        switch (c)
//        {
//        case 'h':
//            options.insert( {"host",optarg} );
//            break;
//        case 'p':
//            options.insert( {"port",optarg} );
//            break;
//        case 't':
//            options.insert( {"type",optarg} );
//            break;
//        case 'w':
//            options.insert( {"write",optarg} );
//            break;
//        default: printf("invalid argument");
//        }
//    }
//    return options;
//}








COptions::COptions(int argc, char **argv)
{
    int c;
    while((c = getopt_long (argc, argv, shortOptions, longOptions, NULL)) != -1)
    {
        switch (c)
        {
        case 'h':
            _options.insert( {"host",optarg} );
            break;
        case 'p':
            _options.insert( {"port",optarg} );
            break;
        case 't':
            _options.insert( {"type",optarg} );
            break;
        case 'w':
            _options.insert( {"write",optarg} );
            break;
        default: printf("invalid argument");throw std::invalid_argument("命令行参数无效");
        }
    }
}

string COptions::find(const string &name)
{
    OptionMap::const_iterator it = _options.find(name);

   if ( it == _options.end() )
   {
       return "";
   }
   return it->second;
}
